<?php
namespace app\modules\epi\controllers;

use Yii;
use yii\db\Expression;
use ac\controllers\AuthController;
use app\models\Experts;
use ac\component\exception\ErrMap;
use ac\component\tool\Tool;
use app\models\OperationLog;
use ac\behaviors\Sort;


class ExpertsController extends AuthController
{
    public function behaviors()
    {
        return [
            Sort::className(),

        ];
    }
    /*内容列表*/
    public function actionExpertsList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $experts = new Experts;
        $count = $experts::find()->count();
        $expertsList = $experts::find()->joinWith('member')
                        ->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        $res = array(
            'list' => $expertsList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加内容*/
    public function actionAddExperts()
    {
        if(Yii::$app->request->isPost){
            $experts = new Experts;
            if($experts->addExperts($this->post)){
                $backInfo = [0, '添加老师成功'];//返回信息
            }else{
                if($experts->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $experts->getErrors();//异常信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }


    /*修改内容*/
    public function actionModifyExperts()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            // P($this->get);
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $experts = Experts::find()->joinWith('member')
                            ->where('id = :id', [':id' => $id])
                            ->asArray()->one();
                // P($experts);
                if(!is_null($experts)){
                    if(!empty($experts['avatar'])){
                        $experts['avatar'] = explode(',', $experts['avatar']);
                    }else{
                        $experts['avatar'] = [];
                    }
                    // P($experts);
                    return Tool::showRes([0, '获取成功'], $experts);
                };
            }
        }


        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['Experts']['id'])?intval($this->post['Experts']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $experts = new Experts;
                if($experts->modifyExperts($this->post, $id)){
                    $backInfo = [0, '修改老师成功'];//返回信息
                }else{
                    if($experts->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $experts->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除内容*/
    public function actionDeleteExperts()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $experts = new Experts;
            if($experts->deleteAll('id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除老师成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除老师失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }

    /*排序*/
    public function actionSetSort()
    {
        if(Yii::$app->request->isPost){
            $sort = $this->post['sort'];
            // P($sort);
            if(!empty($sort) and is_array($sort)){
                if($this->setSortList(new Experts, $sort)){
                    $backInfo = [0, '设置老师排序成功'];//返回信息
                }
            }
            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
